﻿@model LogManager.ViewModels.EventList

@using MvcContrib.UI.Grid;
@using MvcContrib.Pagination;
@using MvcContrib.UI.Pager;
@using LogManager.Helpers;
@using LogManager.Base

@{
	var downloadId = "download-"+Guid.NewGuid().ToString();
}
<div class="events">
    <div class="download">
	    <img src="@Url.Content("~/Content/Images/excel_logo.png")" alt="exceldownload" />
	    <a id="@downloadId" href="@downloadId" >Download events as excel</a>
    </div>
    @(Html.DatePager().Previous("Previous month").Next("Next month"))
    @(Html.Pager(Model.Events).First("First").Previous("Previous").Next("Next").Last("Last"))
    @(Html.Grid<LogManager.Base.IEvent>(Model.Events).Columns(column =>
    {
        column.For(e => e.ServerName).Header("Server").HeaderAttributes(style => "width:100px;");
        column.For(e => Html.ActionLink(e.LogName, "Detail", "Log", new { logName = e.LogName, serverName = e.ServerName }, null)).Named("LogName").Header("Log").HeaderAttributes(style => "width:100px;");
        column.For(e => Html.Truncate(e.Message,240)).Named("Message").HeaderAttributes(style => "width:200px;");
        column.For(e => e.User).HeaderAttributes(style => "width:100px;");
        column.For(e => e.TimeStamp).Format("{0:dd.MM.yy HH:mm}").HeaderAttributes(style => "width:100px;");
        column.For(e => e.Properties.GetValueOrDefault("ExceptionType", null)).Named("Exception").Sortable(false);
        column.For(e => e.Properties.GetValueOrDefault("RequestUrl", null)).Named("RequestUrl").HeaderAttributes(style => "width:150px;").Sortable(false);
        column.For(e => Html.Truncate(e.Properties.GetValueOrDefault("ExceptionDetail", null), 240)).Named("ExceptionDetail").HeaderAttributes(style => "width:200px;").Sortable(false);
        column.For(e => Html.ActionLink("Detail", "Detail", "Event", new { serverName = e.ServerName, logName = e.LogName, id = e.Id.Split('/')[3] },null)).Sortable(false).Named("Detail");        
    }).Sort(Model.SortOptions))
    @(Html.Pager(Model.Events).First("First").Previous("Previous").Next("Next").Last("Last"))
    <br />
</div>
<div id="dialog-message" title="Downloading"><p><img src="@Url.Content("~/Content/Images/excel_logo.png")" style="padding-right:5px;" alt="exceldownload" />Your download is being generated. Please wait a moment.</p><img src="@(Url.Content("~/Content/Images/ajax-loader.gif"))" alt="loading" /></div>

<script type="text/javascript">
    jQuery(document).ready(function () {
        $('#dialog-message').hide();
        $('#@downloadId').bind('click', function (event) {
            event.preventDefault();            
            $('#dialog-message').show().dialog({
                modal: true,
                buttons: {
                    Ok: function () {
                        $(this).dialog('close');
                    }
                }
            });
            //setTimeout(function () { $('#dialog-message').dialog('close') }, 5000);
            $.download('@Model.DownloadUrl');
        });

        $.ajaxLinkHandler('.events .pagination a, .events th a', '.events', '.events .grid');
    });
</script>